const globalEvent = weex.requireModule('globalEvent')
// const close = weex.requireModule('close')
globalEvent.addEventListener('androidback', function (e) { // 返回上一页或退出应用
  // close.closeApp()
  const router = require('@/router')
  router.back()
})

export default {
  methods: {
    imgPath (imgName) {
      const platform = weex.config.env.platform
      let imgPath = ''
      // if (process.env.NODE_ENV === 'development' || !process.env.NODE_ENV) {
      //   imgPath = `/src/assets/images/${imgName}`
      // } else if (process.env.NODE_ENV === 'production') {
      //   imgName = imgName.substr(0, imgName.lastIndexOf('.'))
      //   imgPath = `local:///${imgName}`
      // }
      if (platform === 'Web') {
        imgPath = `/src/assets/images/${imgName}`
      } else if (platform === 'android') {
        // imgPath = `/src/assets/images/${imgName}`
        // android 不需要后缀+
        imgName = imgName.substr(0, imgName.lastIndexOf('.'))
        imgPath = `local:///${imgName}`
      } else {
        // imgPath = `../images/${imgName}`
        imgPath = `/src/assets/images/${imgName}`
      }
      return imgPath
    },
    resizeImage (event) {
      let width = event.size.naturalWidth
      let height = event.size.naturalHeight
      event.target.style.width = width + 'px'
      event.target.style.height = height + 'px'
    },
    filterTime (time) {
      let dateTime = new Date(time.replace(/-/g, '/')).getTime()
      let nowTime = new Date().getTime()
      let second = (dateTime - nowTime) / 1000
      let hours = parseInt(second / 60 / 60)
      let day = parseInt(hours / 24)
      let hour = (hours - day * 24) + '小时'
      let str = day + '天' + (day < 100 ? hour : '') + '后截止'
      return str
    },
    getNowDate () { // 获取当前时间
      let date = new Date()
      let sign1 = '-'
      let sign2 = ':'
      let year = date.getFullYear() // 年
      let month = date.getMonth() + 1 // 月
      let day = date.getDate() // 日
      let hour = date.getHours() // 时
      let minutes = date.getMinutes() // 分
      let seconds = date.getSeconds() // 秒
      if (month >= 1 && month <= 9) {
        month = '0' + month
      }
      if (day >= 0 && day <= 9) {
        day = '0' + day
      }
      if (hour >= 0 && hour <= 9) {
        hour = '0' + hour
      }
      if (minutes >= 0 && minutes <= 9) {
        minutes = '0' + minutes
      }
      if (seconds >= 0 && seconds <= 9) {
        seconds = '0' + seconds
      }
      let currentdate = year + sign1 + month + sign1 + day + ' ' + hour + sign2 + minutes + sign2 + seconds
      return currentdate
    }
  }
}
